Source for file PEAR.php

Documentation is available at PEAR.php

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PEAR, the PHP Extension and Application Repository                   |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at the following url:           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Sterling Hughes <sterling@php.net>                          |
  17. // |          Stig Bakken <ssb@php.net>                                   |
  18. // |          Tomas V.V.Cox <cox@idecnet.com>                             |
  19. // +----------------------------------------------------------------------+
  20. //
  21. // $Id: PEAR.php,v 1.1 2005/07/22 01:57:13 eddieajau Exp $
  22. //
  23.  
  24. define('PEAR_ERROR_RETURN',     1);
  25. define('PEAR_ERROR_PRINT',      2);
  26. define('PEAR_ERROR_TRIGGER',    4);
  27. define('PEAR_ERROR_DIE',        8);
  28. define('PEAR_ERROR_CALLBACK',  16);
  29. define('PEAR_ERROR_EXCEPTION'32);
  30. define('PEAR_ZE2'(function_exists('version_compare'&&
  31.                     version_compare(zend_version()"2-dev""ge")));
  32.  
  33. if (substr(PHP_OS03== 'WIN'{
  34.     define('OS_WINDOWS'true);
  35.     define('OS_UNIX',    false);
  36.     define('PEAR_OS',    'Windows');
  37. else {
  38.     define('OS_WINDOWS'false);
  39.     define('OS_UNIX',    true);
  40.     define('PEAR_OS',    'Unix')// blatant assumption
  41. }
  42.  
  43. // instant backwards compatibility
  44. if (!defined('PATH_SEPARATOR')) {
  45.     if (OS_WINDOWS{
  46.         define('PATH_SEPARATOR'';');
  47.     else {
  48.         define('PATH_SEPARATOR'':');
  49.     }
  50. }
  51.  
  52. $GLOBALS['_PEAR_default_error_mode']     PEAR_ERROR_RETURN;
  53. $GLOBALS['_PEAR_default_error_options']  E_USER_NOTICE;
  54. $GLOBALS['_PEAR_destructor_object_list'array();
  55. $GLOBALS['_PEAR_shutdown_funcs']         array();
  56. $GLOBALS['_PEAR_error_handler_stack']    array();
  57.  
  58. ini_set('track_errors'true);
  59.  
  60. /**
  61.  * Base class for other PEAR classes.  Provides rudimentary
  62.  * emulation of destructors.
  63.  *
  64.  * If you want a destructor in your class, inherit PEAR and make a
  65.  * destructor method called _yourclassname (same name as the
  66.  * constructor, but with a "_" prefix).  Also, in your constructor you
  67.  * have to call the PEAR constructor: $this->PEAR();.
  68.  * The destructor method will be called without parameters.  Note that
  69.  * at in some SAPI implementations (such as Apache), any output during
  70.  * the request shutdown (in which destructors are called) seems to be
  71.  * discarded.  If you need to get any debug information from your
  72.  * destructor, use error_log(), syslog() or something similar.
  73.  *
  74.  * IMPORTANT! To use the emulated destructors you need to create the
  75.  * objects by reference: $obj =& new PEAR_child;
  76.  *
  77.  * @since PHP 4.0.2
  78.  * @author Stig Bakken <ssb@php.net>
  79.  * @see http://pear.php.net/manual/
  80.  */
  81. class PEAR
  82. {
  83.     // {{{ properties
  84.  
  85.     /**
  86.      * Whether to enable internal debug messages.
  87.      *
  88.      * @var     bool 
  89.      * @access  private
  90.      */
  91.     var $_debug false;
  92.  
  93.     /**
  94.      * Default error mode for this object.
  95.      *
  96.      * @var     int 
  97.      * @access  private
  98.      */
  99.     var $_default_error_mode null;
  100.  
  101.     /**
  102.      * Default error options used for this object when error mode
  103.      * is PEAR_ERROR_TRIGGER.
  104.      *
  105.      * @var     int 
  106.      * @access  private
  107.      */
  108.     var $_default_error_options null;
  109.  
  110.     /**
  111.      * Default error handler (callback) for this object, if error mode is
  112.      * PEAR_ERROR_CALLBACK.
  113.      *
  114.      * @var     string 
  115.      * @access  private
  116.      */
  117.     var $_default_error_handler '';
  118.  
  119.     /**
  120.      * Which class to use for error objects.
  121.      *
  122.      * @var     string 
  123.      * @access  private
  124.      */
  125.     var $_error_class 'PEAR_Error';
  126.  
  127.     /**
  128.      * An array of expected errors.
  129.      *
  130.      * @var     array 
  131.      * @access  private
  132.      */
  133.     var $_expected_errors array();
  134.  
  135.     // }}}
  136.  
  137.     // {{{ constructor
  138.  
  139.     /**
  140.      * Constructor.  Registers this object in
  141.      * $_PEAR_destructor_object_list for destructor emulation if a
  142.      * destructor object exists.
  143.      *
  144.      * @param string $error_class  (optional) which class to use for
  145.      *         error objects, defaults to PEAR_Error.
  146.      * @access public
  147.      * @return void 
  148.      */
  149.     function PEAR($error_class null)
  150.     {
  151.         $classname get_class($this);
  152.         if ($this->_debug{
  153.             print "PEAR constructor called, class=$classname\n";
  154.         }
  155.         if ($error_class !== null{
  156.             $this->_error_class $error_class;
  157.         }
  158.         while ($classname{
  159.             $destructor "_$classname";
  160.             if (method_exists($this$destructor)) {
  161.                 global $_PEAR_destructor_object_list;
  162.                 $_PEAR_destructor_object_list[&$this;
  163.                 break;
  164.             else {
  165.                 $classname get_parent_class($classname);
  166.             }
  167.         }
  168.     }
  169.  
  170.     // }}}
  171.     // {{{ destructor
  172.  
  173.     /**
  174.      * Destructor (the emulated type of...).  Does nothing right now,
  175.      * but is included for forward compatibility, so subclass
  176.      * destructors should always call it.
  177.      *
  178.      * See the note in the class desciption about output from
  179.      * destructors.
  180.      *
  181.      * @access public
  182.      * @return void 
  183.      */
  184.     function _PEAR({
  185.         if ($this->_debug{
  186.             printf("PEAR destructor called, class=%s\n"get_class($this));
  187.         }
  188.     }
  189.  
  190.     // }}}
  191.     // {{{ getStaticProperty()
  192.  
  193.     /**
  194.     * If you have a class that's mostly/entirely static, and you need static
  195.     * properties, you can use this method to simulate them. Eg. in your method(s)
  196.     * do this: $myVar = &PEAR::getStaticProperty('myVar');
  197.     * You MUST use a reference, or they will not persist!
  198.     *
  199.     * @access public
  200.     * @param  string $class  The calling classname, to prevent clashes
  201.     * @param  string $var    The variable to retrieve.
  202.     * @return mixed   A reference to the variable. If not set it will be
  203.     *                  auto initialised to NULL.
  204.     */
  205.     function &getStaticProperty($class$var)
  206.     {
  207.         static $properties;
  208.         return $properties[$class][$var];
  209.     }
  210.  
  211.     // }}}
  212.     // {{{ registerShutdownFunc()
  213.  
  214.     /**
  215.     * Use this function to register a shutdown method for static
  216.     * classes.
  217.     *
  218.     * @access public
  219.     * @param  mixed $func  The function name (or array of class/method) to call
  220.     * @param  mixed $args  The arguments to pass to the function
  221.     * @return void 
  222.     */
  223.     function registerShutdownFunc($func$args array())
  224.     {
  225.         $GLOBALS['_PEAR_shutdown_funcs'][array($func$args);
  226.     }
  227.  
  228.     // }}}
  229.     // {{{ isError()
  230.  
  231.     /**
  232.      * Tell whether a value is a PEAR error.
  233.      *
  234.      * @param   mixed $data   the value to test
  235.      * @param   int   $code   if $data is an error object, return true
  236.      *                         only if $code is a string and
  237.      *                         $obj->getMessage() == $code or
  238.      *                         $code is an integer and $obj->getCode() == $code
  239.      * @access  public
  240.      * @return  bool    true if parameter is an error
  241.      */
  242.     function isError($data$code null)
  243.     {
  244.         if (is_a($data'PEAR_Error')) {
  245.             if (is_null($code)) {
  246.                 return true;
  247.             elseif (is_string($code)) {
  248.                 return $data->getMessage(== $code;
  249.             else {
  250.                 return $data->getCode(== $code;
  251.             }
  252.         }
  253.         return false;
  254.     }
  255.  
  256.     // }}}
  257.     // {{{ setErrorHandling()
  258.  
  259.     /**
  260.      * Sets how errors generated by this object should be handled.
  261.      * Can be invoked both in objects and statically.  If called
  262.      * statically, setErrorHandling sets the default behaviour for all
  263.      * PEAR objects.  If called in an object, setErrorHandling sets
  264.      * the default behaviour for that object.
  265.      *
  266.      * @param int $mode 
  267.      *         One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
  268.      *         PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE,
  269.      *         PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION.
  270.      *
  271.      * @param mixed $options 
  272.      *         When $mode is PEAR_ERROR_TRIGGER, this is the error level (one
  273.      *         of E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
  274.      *
  275.      *         When $mode is PEAR_ERROR_CALLBACK, this parameter is expected
  276.      *         to be the callback function or method.  A callback
  277.      *         function is a string with the name of the function, a
  278.      *         callback method is an array of two elements: the element
  279.      *         at index 0 is the object, and the element at index 1 is
  280.      *         the name of the method to call in the object.
  281.      *
  282.      *         When $mode is PEAR_ERROR_PRINT or PEAR_ERROR_DIE, this is
  283.      *         a printf format string used when printing the error
  284.      *         message.
  285.      *
  286.      * @access public
  287.      * @return void 
  288.      * @see PEAR_ERROR_RETURN
  289.      * @see PEAR_ERROR_PRINT
  290.      * @see PEAR_ERROR_TRIGGER
  291.      * @see PEAR_ERROR_DIE
  292.      * @see PEAR_ERROR_CALLBACK
  293.      * @see PEAR_ERROR_EXCEPTION
  294.      *
  295.      * @since PHP 4.0.5
  296.      */
  297.  
  298.     function setErrorHandling($mode null$options null)
  299.     {
  300.         if (isset($this&& is_a($this'PEAR')) {
  301.             $setmode     &$this->_default_error_mode;
  302.             $setoptions  &$this->_default_error_options;
  303.         else {
  304.             $setmode     &$GLOBALS['_PEAR_default_error_mode'];
  305.             $setoptions  &$GLOBALS['_PEAR_default_error_options'];
  306.         }
  307.  
  308.         switch ($mode{
  309.             case PEAR_ERROR_RETURN:
  310.             case PEAR_ERROR_PRINT:
  311.             case PEAR_ERROR_TRIGGER:
  312.             case PEAR_ERROR_DIE:
  313.             case PEAR_ERROR_EXCEPTION:
  314.             case null:
  315.                 $setmode $mode;
  316.                 $setoptions $options;
  317.                 break;
  318.  
  319.             case PEAR_ERROR_CALLBACK:
  320.                 $setmode $mode;
  321.                 // class/object method callback
  322.                 if (is_callable($options)) {
  323.                     $setoptions $options;
  324.                 else {
  325.                     trigger_error("invalid error callback"E_USER_WARNING);
  326.                 }
  327.                 break;
  328.  
  329.             default:
  330.                 trigger_error("invalid error mode"E_USER_WARNING);
  331.                 break;
  332.         }
  333.     }
  334.  
  335.     // }}}
  336.     // {{{ expectError()
  337.  
  338.     /**
  339.      * This method is used to tell which errors you expect to get.
  340.      * Expected errors are always returned with error mode
  341.      * PEAR_ERROR_RETURN.  Expected error codes are stored in a stack,
  342.      * and this method pushes a new element onto it.  The list of
  343.      * expected errors are in effect until they are popped off the
  344.      * stack with the popExpect() method.
  345.      *
  346.      * Note that this method can not be called statically
  347.      *
  348.      * @param mixed $code a single error code or an array of error codes to expect
  349.      *
  350.      * @return int     the new depth of the "expected errors" stack
  351.      * @access public
  352.      */
  353.     function expectError($code '*')
  354.     {
  355.         if (is_array($code)) {
  356.             array_push($this->_expected_errors$code);
  357.         else {
  358.             array_push($this->_expected_errorsarray($code));
  359.         }
  360.         return sizeof($this->_expected_errors);
  361.     }
  362.  
  363.     // }}}
  364.     // {{{ popExpect()
  365.  
  366.     /**
  367.      * This method pops one element off the expected error codes
  368.      * stack.
  369.      *
  370.      * @return array   the list of error codes that were popped
  371.      */
  372.     function popExpect()
  373.     {
  374.         return array_pop($this->_expected_errors);
  375.     }
  376.  
  377.     // }}}
  378.     // {{{ _checkDelExpect()
  379.  
  380.     /**
  381.      * This method checks unsets an error code if available
  382.      *
  383.      * @param mixed error code
  384.      * @return bool true if the error code was unset, false otherwise
  385.      * @access private
  386.      * @since PHP 4.3.0
  387.      */
  388.     function _checkDelExpect($error_code)
  389.     {
  390.         $deleted false;
  391.  
  392.         foreach ($this->_expected_errors AS $key => $error_array{
  393.             if (in_array($error_code$error_array)) {
  394.                 unset($this->_expected_errors[$key][array_search($error_code$error_array)]);
  395.                 $deleted true;
  396.             }
  397.  
  398.             // clean up empty arrays
  399.             if (== count($this->_expected_errors[$key])) {
  400.                 unset($this->_expected_errors[$key]);
  401.             }
  402.         }
  403.         return $deleted;
  404.     }
  405.  
  406.     // }}}
  407.     // {{{ delExpect()
  408.  
  409.     /**
  410.      * This method deletes all occurences of the specified element from
  411.      * the expected error codes stack.
  412.      *
  413.      * @param  mixed $error_code error code that should be deleted
  414.      * @return mixed list of error codes that were deleted or error
  415.      * @access public
  416.      * @since PHP 4.3.0
  417.      */
  418.     function delExpect($error_code)
  419.     {
  420.         $deleted false;
  421.  
  422.         if ((is_array($error_code&& (!= count($error_code)))) {
  423.             // $error_code is a non-empty array here;
  424.             // we walk through it trying to unset all
  425.             // values
  426.             foreach($error_code as $key => $error{
  427.                 if ($this->_checkDelExpect($error)) {
  428.                     $deleted =  true;
  429.                 else {
  430.                     $deleted false;
  431.                 }
  432.             }
  433.             return $deleted true PEAR::raiseError("The expected error you submitted does not exist")// IMPROVE ME
  434.         elseif (!empty($error_code)) {
  435.             // $error_code comes alone, trying to unset it
  436.             if ($this->_checkDelExpect($error_code)) {
  437.                 return true;
  438.             else {
  439.                 return PEAR::raiseError("The expected error you submitted does not exist")// IMPROVE ME
  440.             }
  441.         else {
  442.             // $error_code is empty
  443.             return PEAR::raiseError("The expected error you submitted is empty")// IMPROVE ME
  444.         }
  445.     }
  446.  
  447.     // }}}
  448.     // {{{ raiseError()
  449.  
  450.     /**
  451.      * This method is a wrapper that returns an instance of the
  452.      * configured error class with this object's default error
  453.      * handling applied.  If the $mode and $options parameters are not
  454.      * specified, the object's defaults are used.
  455.      *
  456.      * @param mixed $message a text error message or a PEAR error object
  457.      *
  458.      * @param int $code      a numeric error code (it is up to your class
  459.      *                   to define these if you want to use codes)
  460.      *
  461.      * @param int $mode      One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
  462.      *                   PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE,
  463.      *                   PEAR_ERROR_CALLBACK, PEAR_ERROR_EXCEPTION.
  464.      *
  465.      * @param mixed $options If $mode is PEAR_ERROR_TRIGGER, this parameter
  466.      *                   specifies the PHP-internal error level (one of
  467.      *                   E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
  468.      *                   If $mode is PEAR_ERROR_CALLBACK, this
  469.      *                   parameter specifies the callback function or
  470.      *                   method.  In other error modes this parameter
  471.      *                   is ignored.
  472.      *
  473.      * @param string $userinfo If you need to pass along for example debug
  474.      *                   information, this parameter is meant for that.
  475.      *
  476.      * @param string $error_class The returned error object will be
  477.      *                   instantiated from this class, if specified.
  478.      *
  479.      * @param bool $skipmsg If true, raiseError will only pass error codes,
  480.      *                   the error message parameter will be dropped.
  481.      *
  482.      * @access public
  483.      * @return object   PEAR error object
  484.      * @see PEAR::setErrorHandling
  485.      * @since PHP 4.0.5
  486.      */
  487.     function raiseError($message null,
  488.                          $code null,
  489.                          $mode null,
  490.                          $options null,
  491.                          $userinfo null,
  492.                          $error_class null,
  493.                          $skipmsg false)
  494.     {
  495.         // The error is yet a PEAR error object
  496.         if (is_object($message)) {
  497.             $code        $message->getCode();
  498.             $userinfo    $message->getUserInfo();
  499.             $error_class $message->getType();
  500.             $message->error_message_prefix '';
  501.             $message     $message->getMessage();
  502.         }
  503.  
  504.         if (isset($this&& isset($this->_expected_errors&& sizeof($this->_expected_errors&& sizeof($exp end($this->_expected_errors))) {
  505.             if ($exp[0== "*" ||
  506.                 (is_int(reset($exp)) && in_array($code$exp)) ||
  507.                 (is_string(reset($exp)) && in_array($message$exp))) {
  508.                 $mode PEAR_ERROR_RETURN;
  509.             }
  510.         }
  511.         // No mode given, try global ones
  512.         if ($mode === null{
  513.             // Class error handler
  514.             if (isset($this&& isset($this->_default_error_mode)) {
  515.                 $mode    $this->_default_error_mode;
  516.                 $options $this->_default_error_options;
  517.             // Global error handler
  518.             elseif (isset($GLOBALS['_PEAR_default_error_mode'])) {
  519.                 $mode    $GLOBALS['_PEAR_default_error_mode'];
  520.                 $options $GLOBALS['_PEAR_default_error_options'];
  521.             }
  522.         }
  523.  
  524.         if ($error_class !== null{
  525.             $ec $error_class;
  526.         elseif (isset($this&& isset($this->_error_class)) {
  527.             $ec $this->_error_class;
  528.         else {
  529.             $ec 'PEAR_Error';
  530.         }
  531.         if ($skipmsg{
  532.             return new $ec($code$mode$options$userinfo);
  533.         else {
  534.             return new $ec($message$code$mode$options$userinfo);
  535.         }
  536.     }
  537.  
  538.     // }}}
  539.     // {{{ throwError()
  540.  
  541.     /**
  542.      * Simpler form of raiseError with fewer options.  In most cases
  543.      * message, code and userinfo are enough.
  544.      *
  545.      * @param string $message 
  546.      *
  547.      */
  548.     function throwError($message null,
  549.                          $code null,
  550.                          $userinfo null)
  551.     {
  552.         if (isset($this&& is_subclass_of($this'PEAR_Error')) {
  553.             return $this->raiseError($message$codenullnull$userinfo);
  554.         else {
  555.             return PEAR::raiseError($message$codenullnull$userinfo);
  556.         }
  557.     }
  558.  
  559.     // }}}
  560.     // {{{ pushErrorHandling()
  561.  
  562.     /**
  563.      * Push a new error handler on top of the error handler options stack. With this
  564.      * you can easily override the actual error handler for some code and restore
  565.      * it later with popErrorHandling.
  566.      *
  567.      * @param mixed $mode (same as setErrorHandling)
  568.      * @param mixed $options (same as setErrorHandling)
  569.      *
  570.      * @return bool Always true
  571.      *
  572.      * @see PEAR::setErrorHandling
  573.      */
  574.     function pushErrorHandling($mode$options null)
  575.     {
  576.         $stack &$GLOBALS['_PEAR_error_handler_stack'];
  577.         if (isset($this&& is_a($this'PEAR')) {
  578.             $def_mode    &$this->_default_error_mode;
  579.             $def_options &$this->_default_error_options;
  580.         else {
  581.             $def_mode    &$GLOBALS['_PEAR_default_error_mode'];
  582.             $def_options &$GLOBALS['_PEAR_default_error_options'];
  583.         }
  584.         $stack[array($def_mode$def_options);
  585.  
  586.         if (isset($this&& is_a($this'PEAR')) {
  587.             $this->setErrorHandling($mode$options);
  588.         else {
  589.             PEAR::setErrorHandling($mode$options);
  590.         }
  591.         $stack[array($mode$options);
  592.         return true;
  593.     }
  594.  
  595.     // }}}
  596.     // {{{ popErrorHandling()
  597.  
  598.     /**
  599.     * Pop the last error handler used
  600.     *
  601.     * @return bool Always true
  602.     *
  603.     * @see PEAR::pushErrorHandling
  604.     */
  605.     function popErrorHandling()
  606.     {
  607.         $stack &$GLOBALS['_PEAR_error_handler_stack'];
  608.         array_pop($stack);
  609.         list($mode$options$stack[sizeof($stack1];
  610.         array_pop($stack);
  611.         if (isset($this&& is_a($this'PEAR')) {
  612.             $this->setErrorHandling($mode$options);
  613.         else {
  614.             PEAR::setErrorHandling($mode$options);
  615.         }
  616.         return true;
  617.     }
  618.  
  619.     // }}}
  620.     // {{{ loadExtension()
  621.  
  622.     /**
  623.     * OS independant PHP extension load. Remember to take care
  624.     * on the correct extension name for case sensitive OSes.
  625.     *
  626.     * @param string $ext The extension name
  627.     * @return bool Success or not on the dl() call
  628.     */
  629.     function loadExtension($ext)
  630.     {
  631.         if (!extension_loaded($ext)) {
  632.             // if either returns true dl() will produce a FATAL error, stop that
  633.             if ((ini_get('enable_dl'!= 1|| (ini_get('safe_mode'== 1)) {
  634.                 return false;
  635.             }
  636.             if (OS_WINDOWS{
  637.                 $suffix '.dll';
  638.             elseif (PHP_OS == 'HP-UX'{
  639.                 $suffix '.sl';
  640.             elseif (PHP_OS == 'AIX'{
  641.                 $suffix '.a';
  642.             elseif (PHP_OS == 'OSX'{
  643.                 $suffix '.bundle';
  644.             else {
  645.                 $suffix '.so';
  646.             }
  647.             return @dl('php_'.$ext.$suffix|| @dl($ext.$suffix);
  648.         }
  649.         return true;
  650.     }
  651.  
  652.     // }}}
  653. }
  654.  
  655. // {{{ _PEAR_call_destructors()
  656.  
  657. {
  658.     global $_PEAR_destructor_object_list;
  659.     if (is_array($_PEAR_destructor_object_list&&
  660.         sizeof($_PEAR_destructor_object_list))
  661.     {
  662.         reset($_PEAR_destructor_object_list);
  663.         while (list($k$objrefeach($_PEAR_destructor_object_list)) {
  664.             $classname get_class($objref);
  665.             while ($classname{
  666.                 $destructor "_$classname";
  667.                 if (method_exists($objref$destructor)) {
  668.                     $objref->$destructor();
  669.                     break;
  670.                 else {
  671.                     $classname get_parent_class($classname);
  672.                 }
  673.             }
  674.         }
  675.         // Empty the object list to ensure that destructors are
  676.         // not called more than once.
  677.         $_PEAR_destructor_object_list array();
  678.     }
  679.  
  680.     // Now call the shutdown functions
  681.     if (is_array($GLOBALS['_PEAR_shutdown_funcs']AND !empty($GLOBALS['_PEAR_shutdown_funcs'])) {
  682.         foreach ($GLOBALS['_PEAR_shutdown_funcs'as $value{
  683.             call_user_func_array($value[0]$value[1]);
  684.         }
  685.     }
  686. }
  687.  
  688. // }}}
  689.  
  690. class PEAR_Error
  691. {
  692.     // {{{ properties
  693.  
  694.     var $error_message_prefix = '';
  695.     var $mode                 = PEAR_ERROR_RETURN;
  696.     var $level                = E_USER_NOTICE;
  697.     var $code                 = -1;
  698.     var $message              = '';
  699.     var $userinfo             = '';
  700.     var $backtrace            = null;
  701.  
  702.     // }}}
  703.     // {{{ constructor
  704.  
  705.     /**
  706.      * PEAR_Error constructor
  707.      *
  708.      * @param string $message  message
  709.      *
  710.      * @param int $code     (optional) error code
  711.      *
  712.      * @param int $mode     (optional) error mode, one of: PEAR_ERROR_RETURN,
  713.      *  PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER,
  714.      *  PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION
  715.      *
  716.      * @param mixed $options   (optional) error level, _OR_ in the case of
  717.      *  PEAR_ERROR_CALLBACK, the callback function or object/method
  718.      *  tuple.
  719.      *
  720.      * @param string $userinfo (optional) additional user/debug info
  721.      *
  722.      * @access public
  723.      *
  724.      */
  725.     function PEAR_Error($message 'unknown error'$code null,
  726.                         $mode null$options null$userinfo null)
  727.     {
  728.         if ($mode === null{
  729.             $mode PEAR_ERROR_RETURN;
  730.         }
  731.         $this->message   = $message;
  732.         $this->code      = $code;
  733.         $this->mode      = $mode;
  734.         $this->userinfo  = $userinfo;
  735.         if (function_exists("debug_backtrace")) {
  736.             $this->backtrace = debug_backtrace();
  737.         }
  738.         if ($mode PEAR_ERROR_CALLBACK{
  739.             $this->level = E_USER_NOTICE;
  740.             $this->callback $options;
  741.         else {
  742.             if ($options === null{
  743.                 $options E_USER_NOTICE;
  744.             }
  745.             $this->level = $options;
  746.             $this->callback null;
  747.         }
  748.         if ($this->mode PEAR_ERROR_PRINT{
  749.             if (is_null($options|| is_int($options)) {
  750.                 $format "%s";
  751.             else {
  752.                 $format $options;
  753.             }
  754.             printf($format$this->getMessage());
  755.         }
  756.         if ($this->mode PEAR_ERROR_TRIGGER{
  757.             trigger_error($this->getMessage()$this->level);
  758.         }
  759.         if ($this->mode PEAR_ERROR_DIE{
  760.             $msg $this->getMessage();
  761.             if (is_null($options|| is_int($options)) {
  762.                 $format "%s";
  763.                 if (substr($msg-1!= "\n"{
  764.                     $msg .= "\n";
  765.                 }
  766.             else {
  767.                 $format $options;
  768.             }
  769.             die(sprintf($format$msg));
  770.         }
  771.         if ($this->mode PEAR_ERROR_CALLBACK{
  772.             if (is_callable($this->callback)) {
  773.                 call_user_func($this->callback$this);
  774.             }
  775.         }
  776.         if (PEAR_ZE2 && $this->mode PEAR_ERROR_EXCEPTION{
  777.             eval('throw $this;');
  778.         }
  779.     }
  780.  
  781.     // }}}
  782.     // {{{ getMode()
  783.  
  784.     /**
  785.      * Get the error mode from an error object.
  786.      *
  787.      * @return int error mode
  788.      * @access public
  789.      */
  790.     function getMode({
  791.         return $this->mode;
  792.     }
  793.  
  794.     // }}}
  795.     // {{{ getCallback()
  796.  
  797.     /**
  798.      * Get the callback function/method from an error object.
  799.      *
  800.      * @return mixed callback function or object/method array
  801.      * @access public
  802.      */
  803.     function getCallback({
  804.         return $this->callback;
  805.     }
  806.  
  807.     // }}}
  808.     // {{{ getMessage()
  809.  
  810.  
  811.     /**
  812.      * Get the error message from an error object.
  813.      *
  814.      * @return  string  full error message
  815.      * @access public
  816.      */
  817.     function getMessage()
  818.     {
  819.         return ($this->error_message_prefix . $this->message);
  820.     }
  821.  
  822.  
  823.     // }}}
  824.     // {{{ getCode()
  825.  
  826.     /**
  827.      * Get error code from an error object
  828.      *
  829.      * @return int error code
  830.      * @access public
  831.      */
  832.      function getCode()
  833.      {
  834.         return $this->code;
  835.      }
  836.  
  837.     // }}}
  838.     // {{{ getType()
  839.  
  840.     /**
  841.      * Get the name of this error/exception.
  842.      *
  843.      * @return string error/exception name (type)
  844.      * @access public
  845.      */
  846.     function getType()
  847.     {
  848.         return get_class($this);
  849.     }
  850.  
  851.     // }}}
  852.     // {{{ getUserInfo()
  853.  
  854.     /**
  855.      * Get additional user-supplied information.
  856.      *
  857.      * @return string user-supplied information
  858.      * @access public
  859.      */
  860.     function getUserInfo()
  861.     {
  862.         return $this->userinfo;
  863.     }
  864.  
  865.     // }}}
  866.     // {{{ getDebugInfo()
  867.  
  868.     /**
  869.      * Get additional debug information supplied by the application.
  870.      *
  871.      * @return string debug information
  872.      * @access public
  873.      */
  874.     function getDebugInfo()
  875.     {
  876.         return $this->getUserInfo();
  877.     }
  878.  
  879.     // }}}
  880.     // {{{ getBacktrace()
  881.  
  882.     /**
  883.      * Get the call backtrace from where the error was generated.
  884.      * Supported with PHP 4.3.0 or newer.
  885.      *
  886.      * @param int $frame (optional) what frame to fetch
  887.      * @return array Backtrace, or NULL if not available.
  888.      * @access public
  889.      */
  890.     function getBacktrace($frame null)
  891.     {
  892.         if ($frame === null{
  893.             return $this->backtrace;
  894.         }
  895.         return $this->backtrace[$frame];
  896.     }
  897.  
  898.     // }}}
  899.     // {{{ addUserInfo()
  900.  
  901.     function addUserInfo($info)
  902.     {
  903.         if (empty($this->userinfo)) {
  904.             $this->userinfo = $info;
  905.         else {
  906.             $this->userinfo .= " ** $info";
  907.         }
  908.     }
  909.  
  910.     // }}}
  911.     // {{{ toString()
  912.  
  913.     /**
  914.      * Make a string representation of this object.
  915.      *
  916.      * @return string a string with an object summary
  917.      * @access public
  918.      */
  919.     function toString({
  920.         $modes array();
  921.         $levels array(E_USER_NOTICE  => 'notice',
  922.                         E_USER_WARNING => 'warning',
  923.                         E_USER_ERROR   => 'error');
  924.         if ($this->mode PEAR_ERROR_CALLBACK{
  925.             if (is_array($this->callback)) {
  926.                 $callback get_class($this->callback[0]'::' .
  927.                     $this->callback[1];
  928.             else {
  929.                 $callback $this->callback;
  930.             }
  931.             return sprintf('[%s: message="%s" code=%d mode=callback '.
  932.                            'callback=%s prefix="%s" info="%s"]',
  933.                            get_class($this)$this->message$this->code,
  934.                            $callback$this->error_message_prefix,
  935.                            $this->userinfo);
  936.         }
  937.         if ($this->mode PEAR_ERROR_PRINT{
  938.             $modes['print';
  939.         }
  940.         if ($this->mode PEAR_ERROR_TRIGGER{
  941.             $modes['trigger';
  942.         }
  943.         if ($this->mode PEAR_ERROR_DIE{
  944.             $modes['die';
  945.         }
  946.         if ($this->mode PEAR_ERROR_RETURN{
  947.             $modes['return';
  948.         }
  949.         return sprintf('[%s: message="%s" code=%d mode=%s level=%s '.
  950.                        'prefix="%s" info="%s"]',
  951.                        get_class($this)$this->message$this->code,
  952.                        implode("|"$modes)$levels[$this->level],
  953.                        $this->error_message_prefix,
  954.                        $this->userinfo);
  955.     }
  956.  
  957.     // }}}
  958. }
  959.  
  960. register_shutdown_function("_PEAR_call_destructors");
  961.  
  962. /*
  963.  * Local Variables:
  964.  * mode: php
  965.  * tab-width: 4
  966.  * c-basic-offset: 4
  967.  * End:
  968.  */
  969. ?>

Documentation generated on Mon, 05 May 2008 16:22:14 +0400 by phpDocumentor 1.4.0